{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "QUANTAXIS>> start QUANTAXIS\n",
      "QUANTAXIS>> ip:127.0.0.1,port:27017\n",
      "QUANTAXIS>> Selecting the Best Server IP of TDX\n",
      "QUANTAXIS>> === The BEST SERVER ===\n",
      " stock_ip 115.238.90.165 future_ip 61.152.107.141\n",
      "QUANTAXIS>> Welcome to QUANTAXIS, the Version is remake-version\n",
      "QUANTAXIS>>  \n",
      " ```````````````````````````````````````````````````````````````````````````````````````````````````````````````````````` \n",
      "  ``########`````##````````##``````````##`````````####````````##```##########````````#``````##``````###```##`````######`` \n",
      "  `##``````## ```##````````##`````````####````````##`##```````##```````##```````````###``````##````##`````##```##`````##` \n",
      "  ##````````##```##````````##````````##`##````````##``##``````##```````##``````````####```````#```##``````##```##``````## \n",
      "  ##````````##```##````````##```````##```##```````##```##`````##```````##`````````##`##```````##`##```````##````##``````` \n",
      "  ##````````##```##````````##``````##`````##``````##````##````##```````##````````##``###```````###````````##`````##`````` \n",
      "  ##````````##```##````````##``````##``````##`````##`````##```##```````##```````##````##```````###````````##``````###```` \n",
      "  ##````````##```##````````##`````##````````##````##``````##``##```````##``````##``````##`````##`##```````##````````##``` \n",
      "  ##````````##```##````````##````#############````##```````##`##```````##`````###########`````##``##``````##`````````##`` \n",
      "  ###```````##```##````````##```##```````````##```##```````##`##```````##````##`````````##```##```##``````##```##`````##` \n",
      "  `##``````###````##``````###``##`````````````##``##````````####```````##```##``````````##``###````##`````##````##`````## \n",
      "  ``#########``````########```##``````````````###`##``````````##```````##``##````````````##`##``````##````##`````##````## \n",
      "  ````````#####`````````````````````````````````````````````````````````````````````````````````````````````````````####` \n",
      "  ``````````````````````````````````````````````````````````````````````````````````````````````````````````````````````` \n",
      "  ``````````````````````````Copyright``yutiansut``2017``````QUANTITATIVE FINANCIAL FRAMEWORK````````````````````````````` \n",
      "  ``````````````````````````````````````````````````````````````````````````````````````````````````````````````````````` \n",
      " ```````````````````````````````````````````````````````````````````````````````````````````````````````````````````````` \n",
      " ```````````````````````````````````````````````````````````````````````````````````````````````````````````````````````` \n",
      " \n"
     ]
    }
   ],
   "source": [
    "import QUANTAXIS as QA\n",
    "import threading"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "user = QA.QA_Portfolio()\n",
    "# 创建两个account\n",
    "\n",
    "a_1 = user.new_account()\n",
    "a_2 = user.new_account()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "< QA_Order datetime:2017-12-14 09:31:00 code:000001 price:None towards:1 btype:0x01 order_id:Order_TUp4Wr8F account:Acc_dAiGYNIn status:100 >"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "user.get_account(a_1).send_order(amount=10000, amount_model=QA.AMOUNT_MODEL.BY_MONEY,\n",
    "                    time='2017-12-14', code='000001', order_model=QA.ORDER_MODEL.CLOSE, towards=QA.ORDER_DIRECTION.BUY)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Acc_dAiGYNIn'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a_1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "< QA_MARKET with ['backtest'] Broker >\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# 创建一个交易前置\n",
    "market = QA.QA_Market()\n",
    "# 交易前置连接broker \n",
    "market.start()\n",
    "market.connect(QA.RUNNING_ENVIRONMENT.BACKETEST)\n",
    "\n",
    "# 打印market\n",
    "print(market)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<_MainThread(MainThread, started 20504)>,\n",
       " <Thread(Thread-4, started daemon 13976)>,\n",
       " <Heartbeat(Thread-5, started daemon 14068)>,\n",
       " <HistorySavingThread(IPythonHistorySavingThread, started 3236)>,\n",
       " <ParentPollerWindows(Thread-3, started daemon 2424)>,\n",
       " <Thread(pymongo_server_monitor_thread, started daemon 5496)>,\n",
       " <Thread(pymongo_kill_cursors_thread, started daemon 18048)>,\n",
       "  <QA_ENGINE with ['backtest'] kernals>,\n",
       " < QA_Thread backtest >]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "threading.enumerate()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "market.connect(QA.RUNNING_ENVIRONMENT.SIMULATION)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<_MainThread(MainThread, started 20504)>,\n",
       " <Thread(Thread-4, started daemon 13976)>,\n",
       " <Heartbeat(Thread-5, started daemon 14068)>,\n",
       " <HistorySavingThread(IPythonHistorySavingThread, started 3236)>,\n",
       " <ParentPollerWindows(Thread-3, started daemon 2424)>,\n",
       " <Thread(pymongo_server_monitor_thread, started daemon 5496)>,\n",
       " <Thread(pymongo_kill_cursors_thread, started daemon 18048)>,\n",
       "  <QA_ENGINE with ['backtest', 'simulation'] kernals>,\n",
       " < QA_Thread backtest >,\n",
       " < QA_Thread simulation >]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "threading.enumerate()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Acc_dAiGYNIn', 'Acc_lndDuQEN']\n"
     ]
    }
   ],
   "source": [
    "# 登陆交易\n",
    "market.login(a_1)\n",
    "market.login(a_2)\n",
    "# 打印市场中的交易账户\n",
    "print(market.get_account_id())\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<_MainThread(MainThread, started 20504)>,\n",
       " <Thread(Thread-4, started daemon 13976)>,\n",
       " <Heartbeat(Thread-5, started daemon 14068)>,\n",
       " <HistorySavingThread(IPythonHistorySavingThread, started 3236)>,\n",
       " <ParentPollerWindows(Thread-3, started daemon 2424)>,\n",
       " <Thread(pymongo_server_monitor_thread, started daemon 5496)>,\n",
       " <Thread(pymongo_kill_cursors_thread, started daemon 18048)>,\n",
       "  <QA_ENGINE with ['backtest', 'simulation'] kernals>,\n",
       " < QA_Thread backtest >,\n",
       " < QA_Thread simulation >]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import threading\n",
    "threading.enumerate()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "market.insert_order(account_id=a_1, amount=10000, amount_model=QA.AMOUNT_MODEL.BY_MONEY,time='2017-12-14', code='000001', order_model=QA.ORDER_MODEL.CLOSE, towards=QA.ORDER_DIRECTION.BUY)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "market.insert_order(account_id=a_2, amount=10000, amount_model=QA.AMOUNT_MODEL.BY_MONEY,\n",
    "                    time='2017-12-14', code='000001', order_model=QA.ORDER_MODEL.CLOSE, towards=QA.ORDER_DIRECTION.BUY)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#market.order_handler.order_queue.settle()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: []\n",
       "Index: []"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "market.order_handler.order_queue()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "market.order_handler.order_queue.trade_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: []\n",
       "Index: []"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "market.order_handler.order_queue.pending"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "market._trade()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "QUANTAXIS>> From Engine  <QA_ENGINE with ['backtest', 'simulation'] kernals>: There are still 2 tasks to do\n",
      "QUANTAXIS>> From Engine  <QA_ENGINE with ['backtest', 'simulation'] kernals>: There are still 1 tasks to do\n",
      "QUANTAXIS>> From Engine < QA_Thread backtest >: There are still 2 tasks to do\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ON QUERY\n",
      "[['000001' '13.15' '13.31' '12.91' '13.0' '1001997.0' '2017-12-14']\n",
      " ['000001' '12.9' '12.93' '12.67' '12.72' '1099952.0' '2017-12-15']\n",
      " ['000001' '12.73' '12.91' '12.64' '12.75' '805464.0' '2017-12-18']\n",
      " ['000001' '12.79' '13.32' '12.74' '13.28' '2397942.0' '2017-12-19']\n",
      " ['000001' '13.2' '13.31' '13.13' '13.26' '1106245.0' '2017-12-20']\n",
      " ['000001' '13.18' '13.68' '13.16' '13.54' '1485203.0' '2017-12-21']\n",
      " ['000001' '13.5' '13.63' '13.45' '13.52' '742900.0' '2017-12-22']]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "QUANTAXIS>> From Engine < QA_Thread backtest >: There are still 1 tasks to do\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ON QUERY\n",
      "[['000001' '13.15' '13.31' '12.91' '13.0' '1001997.0' '2017-12-14']\n",
      " ['000001' '12.9' '12.93' '12.67' '12.72' '1099952.0' '2017-12-15']\n",
      " ['000001' '12.73' '12.91' '12.64' '12.75' '805464.0' '2017-12-18']\n",
      " ['000001' '12.79' '13.32' '12.74' '13.28' '2397942.0' '2017-12-19']\n",
      " ['000001' '13.2' '13.31' '13.13' '13.26' '1106245.0' '2017-12-20']\n",
      " ['000001' '13.18' '13.68' '13.16' '13.54' '1485203.0' '2017-12-21']\n",
      " ['000001' '13.5' '13.63' '13.45' '13.52' '742900.0' '2017-12-22']]\n"
     ]
    }
   ],
   "source": [
    "market.query_data(broker_name=QA.BROKER_TYPE.BACKETEST,data_type=QA.FREQUENCE.DAY,market_type=QA.MARKET_TYPE.STOCK_CN,\n",
    "                 code='000001',start='2017-12-14',end='2017-12-30')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
